package tx.audioAndVideoRecorder;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;


public class MyAudioRecorder {
	
	//File extensions
	private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
    private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
    //Directory where files are saved
    private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
    private static String sLastRecorderFile = null;
    private static String mRecorderFolder = null;
    
    private MediaRecorder recorder = null;
    private int currentFormat = 0;
    private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
    private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP }; 
    
    public MyAudioRecorder(){
    	setRecorderFolder(Environment.getExternalStorageDirectory().getPath() + "/" + AUDIO_RECORDER_FOLDER + "/");
    }
    
    public String getFilename(){
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);
        
        if(!file.exists()){
                file.mkdirs();
        }
        //Save last audio file recorded
        sLastRecorderFile = file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat];
        return (sLastRecorderFile);
	}
	
	public void startRecording(){
	        recorder = new MediaRecorder();
	        
	        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	        recorder.setOutputFormat(output_formats[currentFormat]);
	        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
	        //Specifie file name output
	        recorder.setOutputFile(getFilename());
	        
	        recorder.setOnErrorListener(errorListener);
	        recorder.setOnInfoListener(infoListener);
	        
	        try {
	                recorder.prepare();
	                recorder.start();
	        } catch (IllegalStateException e) {
	                e.printStackTrace();
	        } catch (IOException e) {
	                e.printStackTrace();
	        }
	}
	
	public void stopRecording(){
	        if(null != recorder){
	                recorder.stop();
	                recorder.reset();
	                recorder.release();
	                
	                recorder = null;
	        }
	}
	
	public static void setsLastRecorderFile(String sLastRecorderFile) {
		MyAudioRecorder.sLastRecorderFile = sLastRecorderFile;
	}

	public static String getsLastRecorderFile() {
		return sLastRecorderFile;
	}

	public static void setRecorderFolder(String mRecorderFolder) {
		MyAudioRecorder.mRecorderFolder = mRecorderFolder;
	}

	public static String getRecorderFolder() {
		return mRecorderFolder;
	}

	public MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
               // AppLog.logString("Error: " + what + ", " + extra);
        }
	};
	
	public MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
	        @Override
	        public void onInfo(MediaRecorder mr, int what, int extra) {
	               // AppLog.logString("Warning: " + what + ", " + extra);
	        }
	};
}




